package com.lykj.lib_base.base

import android.content.res.Configuration
import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import com.lykj.lib_base.app.BaseApplication
import com.lykj.lib_base.constants.BConstants
import io.reactivex.disposables.Disposable
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.LinkedHashMap


/**
 * ================================================
 * 基础的Activity父类,取消AutoSize适配使用CancelAdapter
 * ================================================
 *
 * @author gosha
 * @date 2022/9/2-11:25
 */
abstract class BaseActivity : AppCompatActivity() {
    open val bodyParams: HashMap<String, Any> = HashMap()
    open val headerParams: HashMap<String, Any> = HashMap()

    //拦截返回
    open var interceptBack: Boolean = false

    private val disposables: LinkedHashMap<String, LinkedList<Disposable>> =
        LinkedHashMap()

    abstract fun initUI()

    abstract fun initData()

    abstract fun setListener()

    //清除 额外请求头,请求体
    open fun clearParam() {
        clearHeaderParam()
        clearBodyParam()
    }

    open fun clearHeaderParam() {
        headerParams.clear()
    }

    open fun clearBodyParam() {
        bodyParams.clear()
    }

    open fun initHeaderParam() {
//        headerParam["username"] = UserInfoUtil.getUserInfo()?.username ?: ""
//        headerParam["token"] = UserInfoUtil.getUserInfo()?.token ?: ""
    }

    open fun initBodyParam() {
        //FIXME:初始化统一修改信息预留处
//        var deviceId = ""
//        deviceId = if (!DeviceUtil.getUniqueDeviceId(this).isNullOrBlank()) {
//            DeviceUtil.getUniqueDeviceId(this)
//        } else {
//            DeviceUtils.getUniqueDeviceId()
//        }

        bodyParams["channelId"] = BaseApplication.instance?.getUserInfo()?.channelId ?: BConstants.AGENT_CODE
        bodyParams["device"] = 2
        bodyParams["imeil"] = BConstants.DEVICE_ID
    }

    open fun setOnInterceptBack(interceptBack: Boolean) {
        this.interceptBack = interceptBack
    }

    @Deprecated("Deprecated in Java")
    override fun onBackPressed() {
        if (interceptBack) {
        } else {
            super.onBackPressed()
        }
    }

    /**
     * 添加RxJava一次性资源监控
     *
     * @param clazz 实例对象
     * @param disposable
     */
    open fun addDisposables(clazz: Any, disposable: Disposable) {
        val instanceName = "${clazz::class.java}@${
            Integer.toHexString(
                System.identityHashCode(
                    clazz
                )
            )
        }"
        if (!TextUtils.isEmpty(instanceName)
        ) {
            var mDisposables: LinkedList<Disposable>? = null
            mDisposables = disposables[instanceName]
            if (null != mDisposables) {
                disposables[instanceName]?.add(disposable)
            } else {
                mDisposables = LinkedList()
                mDisposables.add(disposable)
                disposables[instanceName] = mDisposables
            }
        }
    }


    /**
     * 移除RxJava的一次性资源监控
     *
     * @param clazz 实例对象
     */
    open fun removeDisposables(clazz: Any) {
        val instanceName = "${clazz::class.java}@${
            Integer.toHexString(
                System.identityHashCode(
                    clazz
                )
            )
        }"
        if (!TextUtils.isEmpty(instanceName)) {
            var mDisposables: LinkedList<Disposable>? = null
            mDisposables = disposables[instanceName]
            if (null != mDisposables) {
                for (dis in mDisposables) {
                    dis.dispose()
                }
            }
        }
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0, 0)
    }
}